<#-- Created by IntelliJ IDEA.
 User: Administrator
 Date: 2017/12/6
 Time: 14:00
 To change this template use File | Settings | File Templates.
 用户管理-->
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>用户管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi"/>
    <link rel="stylesheet" href="${re.contextPath}/plugin/layui2.2/css/layui.css">
    <link rel="stylesheet" href="${re.contextPath}/plugin/lenos/main.css">
    <script type="text/javascript" src="${re.contextPath}/plugin/jquery/jquery-3.2.1.min.js"></script>
    <script type="text/javascript" src="${re.contextPath}/plugin/layui2.2/layui.all.js"
            charset="utf-8"></script>
    <script type="text/javascript" src="${re.contextPath}/plugin/tools/tool.js"></script>

</head>

<body>
<div class="lenos-search">
    <div class="select">
        用户名：
        <div class="layui-inline">
            <input class="layui-input" height="20px" id="uname" autocomplete="off">
        </div>
        邮箱：
        <div class="layui-inline">
            <input class="layui-input" height="20px" id="email" autocomplete="off">
        </div>
        真实姓名：
        <div class="layui-inline">
            <input class="layui-input" height="20px" id="realName" autocomplete="off">
        </div>
        角色：
        <div class="layui-inline">
            <select id="roleId" class="layui-select" style="height:30px">
                <option value="">请选择</option>
                <#list roleList as role>
                    <option value="${role.id}">${role.remark}</option>
                </#list>
            </select>
        </div>
		审核状态：
        <div class="layui-inline">
            <select id="auditStatus" class="layui-select" style="height:30px">
                <option value="">请选择</option>
                    <option value="1">审核通过</option>
                    <option value="2">审核拒绝</option>
                    <option value="3">待审核</option>
            </select>
        </div>
        <button class="select-on layui-btn layui-btn-sm" data-type="select"><i class="layui-icon"></i>
        </button>
        <button class="layui-btn layui-btn-sm icon-position-button" id="refresh" style="float: right;"
                data-type="reload">
            <i class="layui-icon">ဂ</i>
        </button>
    </div>

</div>
<div class="layui-col-md12" style="height:40px;margin-top:3px;">
    <div class="layui-btn-group">
        <@shiro.hasPermission name="user:select">
            <button class="layui-btn layui-btn-normal" data-type="add">
                <i class="layui-icon">&#xe608;</i>新增
            </button>
<#--            <button class="layui-btn layui-btn-normal" data-type="tz">-->
<#--                <i class="layui-icon">&#xe608;</i>跳转-->
<#--            </button>-->

        </@shiro.hasPermission>
        <@shiro.hasPermission name="user:select">
            <button class="layui-btn layui-btn-normal" data-type="update">
                <i class="layui-icon">&#xe642;</i>编辑
            </button>
        </@shiro.hasPermission>
        <@shiro.hasPermission name="user:del">
            <button class="layui-btn layui-btn-normal" data-type="detail">
                <i class="layui-icon">&#xe605;</i>查看
            </button>
        </@shiro.hasPermission>
        <@shiro.hasPermission name="user:repass">
            <button class="layui-btn layui-btn-normal" data-type="changePwd">
                <i class="layui-icon">&#xe605;</i>重置密码
            </button>
        </@shiro.hasPermission>
        <@shiro.hasPermission name="user:select">
            <button id="uploadUser" class="layui-btn layui-btn-normal">
                <i class="layui-icon">&#xe681;</i>导入
            </button>

            <button id="uploadUserRole" class="layui-btn layui-btn-normal">
                <i class="layui-icon">&#xe681;</i>角色导入
            </button>

            <button id="downloadUserRole" class="layui-btn layui-btn-normal" onclick="exportUserRoles()">
                <i class="layui-icon">&#xe601;</i>角色导出
            </button>

            <button id="downUserTemp" class="layui-btn layui-btn-normal" data-type="downUserTemp">
                <i class="layui-icon">&#xe601;</i>用户导入模板下载
            </button>
        <#--            <button class="layui-btn layui-btn-normal" data-type="tz">-->
        <#--                <i class="layui-icon">&#xe608;</i>跳转-->
        <#--            </button>-->

        </@shiro.hasPermission>
    </div>
</div>
<table id="userList" class="layui-hide" lay-filter="user"></table>
<script type="text/html" id="barDemo">
    <@shiro.hasPermission name="user:select">
        <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="detail">查看</a>
    </@shiro.hasPermission>
    <@shiro.hasPermission name="user:update">
        <a class="layui-btn layui-btn-xs  layui-btn-normal" lay-event="edit">编辑</a>
    </@shiro.hasPermission>
    {{#  if(d.auditStatus == '3'){ }}
    <@shiro.hasPermission name="user:update">
        <a class="layui-btn layui-btn-xs  layui-btn-info" lay-event="verify">审核</a>
    </@shiro.hasPermission>
    {{#  } else { }}

    {{#  } }}
    <@shiro.hasPermission name="user:del">
        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
    </@shiro.hasPermission>
</script>
<script type="text/html" id="switchTpl">
    <input type="checkbox" name="sex" lay-skin="switch" lay-text="女|男" lay-filter="sexDemo">
</script>
<script type="text/html" id="auditStat">
    {{#  if(d.auditStatus == '1'){ }}
    <img style="width:24px" src="${re.contextPath}/plugin/common/images/right.png">
    {{#  } else { }}
        {{#  if(d.auditStatus == '2'){ }}
        <img style="width:24px" src="${re.contextPath}/plugin/common/images/wrong.png" alt="{{d.auditFailReason}}">
        {{#  } else { }}
            {{#  if(d.auditStatus == '3'){ }}
            <img style="width:24px" src="${re.contextPath}/plugin/common/images/waiting.png">
            {{#  } else { }}

            {{#  } }}
        {{#  } }}
    {{#  } }}
</script>
<script>
    document.onkeydown = function (e) { // 回车提交表单
        var theEvent = window.event || e;
        var code = theEvent.keyCode || theEvent.which;
        if (code == 13) {
            $(".select .select-on").click();
        }
    }
    //加载模块
    layui.use(['jquery', 'formSelects'], function () {
        var formSelects = layui.formSelects;
    });

    var table;
    layui.use(['table','upload'], function () {
        table = layui.table;
        var upload = layui.upload;
        //方法级渲染
        table.render({
            elem: '#userList'
            , url: 'showUserList'
            , cols: [[
                {checkbox: true, fixed: true, width: '5%'}
                , {
                    field: 'username',
                    title: '用户名',
                    width: '10%',
                    sort: true,
                    style: 'background-color: #009688; color: #fff;'
                }
                , {field: 'realName', title: '真实姓名', width: '9%'}
                , {field: 'companyName', title: '所属公司', width: '11%'}
                , {field: 'gxname', title: '供需方', width: '6%'}
                , {field: 'ptname', title: '个人/组织', width: '8%'}
                , {field: 'userRoleNames', title: '角色', width: '11%'}
                , {field: 'ywname', title: '业务类型', width: '9%'}
                , {field: 'phoneNumber', title: '联系方式', width: '11%'}
				, {field: 'auditStatus', title: '审核状态', width: '6%',align: 'center',templet: '#auditStat'}
                , {field: 'right', title: '操作', width: '14%', toolbar: "#barDemo"}
            ]]
            , page: true,
            height: 'full-83',
            done: function(){
                //执行实例
                var uploadInst = upload.render({
                    elem: '#uploadUser' //绑定元素
                    ,url: '/user/addUsers' //上传接口
                    ,accept: 'file'
                    , exts: 'xlsx|xls|'
                    , multiple: false
                    ,done: function(res){
                        //上传完毕回调
                        if(res.flag){
                            layer.msg("导入成功",{icon:1});
                        }else{
                            layer.msg("导入失败！</br>"+res.msg,{icon:2});
                        }
                    }
                    ,error: function(res){
                        //请求异常回调
                        layer.msg("系统异常，请联系管理员",{icon:2});
                    }
                });
                //执行实例角色导入
                var uploadInst = upload.render({
                    elem: '#uploadUserRole' //绑定元素
                    ,url: '/user/addUserRoles' //上传接口
                    ,accept: 'file'
                    , exts: 'xlsx|xls|'
                    , multiple: false
                    ,done: function(res){
                        //上传完毕回调
                        if(res.flag){
                            layer.msg("导入成功",{icon:1});
                        }else{
                            layer.msg("导入失败！</br>"+res.msg,{icon:2});
                        }
                    }
                    ,error: function(res){
                        //请求异常回调
                        layer.msg("系统异常，请联系管理员",{icon:2});
                    }
                });
            }
        });

        var $ = layui.$, active = {
            select: function () {
                var uname = $('#uname').val();
                var email = $('#email').val();
                var realName = $("#realName").val();
                var roleId = $("#roleId").val();
				var auditStatus = $("#auditStatus").val();
                table.reload('userList', {
                    where: {
                        username: uname,
                        email: email,
                        realName: realName,
                        roleId: roleId,
						auditStatus: auditStatus
                    }
                });
            },
            reload: function () {
                $('#uname').val('');
                $('#email').val('');
                $("#realName").val('');
                $("#roleId").val('');
				$("#auditStatus").val('');
                table.reload('userList', {
                    where: {
                        username: null,
                        email: null,
                        realName: null,
                        roleId: null,
						auditStatus: null
                    }
                });
            },
            add: function () {
                add('添加用户', 'showAddUser', 700, 450);
            },
            tz: function () {
                tz('添加用户', 'showAddUser', 700, 450);
            },
            update: function () {
                var checkStatus = table.checkStatus('userList')
                    , data = checkStatus.data;
                if (data.length != 1) {
                    layer.msg('请选择一行编辑,已选[' + data.length + ']行', {icon: 5});
                    return false;
                }
                update('编辑用户', 'updateUser?id=' + data[0].id, 700, 450);
            },
            detail: function () {
                var checkStatus = table.checkStatus('userList')
                    , data = checkStatus.data;
                if (data.length != 1) {
                    layer.msg('请选择一行查看,已选[' + data.length + ']行', {icon: 5});
                    return false;
                }
                detail('查看用户信息', 'updateUser?id=' + data[0].id, 700, 450);
            },
            changePwd: function () {
                var checkStatus = table.checkStatus('userList')
                    , data = checkStatus.data;
                if (data.length != 1) {
                    layer.msg('请选择一个用户,已选[' + data.length + ']行', {icon: 5});
                    return false;
                }
                rePwd('修改密码', 'goRePass?id=' + data[0].id, 500, 250);
            },
            downUserTemp: function(){
                var _href = encodeURI('/download/doc?fileName=用户导入模板.xlsx');
                location.href = _href;
            }
        };

        //监听表格复选框选择
        table.on('checkbox(user)', function (obj) {
            console.log(obj)
        });
        //监听工具条
        table.on('tool(user)', function (obj) {
            var data = obj.data;
            if (obj.event === 'detail') {
                detail('查看用户', 'updateUser?id=' + data.id, 700, 450);
            } else if (obj.event === 'del') {
                layer.confirm('确定删除用户[<label style="color: #00AA91;">' + data.username + '</label>]?', {
                    btn: ['确定', '取消']
                    // btn: ['逻辑删除', '物理删除']
                }, function () {
                    console.log("确定")
                    // toolDelByFlag(data.id, 'userList', true);
                    toolDelByFlag(data.id, 'userList', false);
                    layer.closeAll();
                });
            } else if (obj.event === 'edit') {
                update('编辑用户', 'updateUser?id=' + data.id, 700, 450);
			}else if(obj.event === 'verify'){
                var userId = obj.data.id;
                if(userId){
                    var w = "";
                    var h = "";
                    if (w == null || w == '') {
                        w = ($(window).width());
                    };
                    if (h == null || h == '') {
                        h = ($(window).height());
                    }
                    layer.open({
                        type: 2,
                        area :  [w + 'px', h + 'px'],
                        title: '审核',
                        closeBtn: 1, //不显示关闭按钮
                        shade: 0.8,
                        content: '/sys/personCenter/index?userId='+userId+'&shenhe=1',
                        end: function(){
                            reloadTable();
                        }
                    });
                }
            }
        });

        $('.layui-col-md12 .layui-btn').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });
        $('.select .layui-btn').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

    });

    function rePwd(title, url, w, h) {
        if (title == null || title == '') {
            title = false;
        }
        ;
        if (url == null || url == '') {
            url = "404.html";
        }
        ;
        if (w == null || w == '') {
            w = ($(window).width() * 0.9);
        }
        ;
        if (h == null || h == '') {
            h = ($(window).height() - 50);
        }
        ;
        layer.open({
            id: 'user-rePwd',
            type: 2,
            area: [w + 'px', h + 'px'],
            fix: false,
            maxmin: true,
            shadeClose: true,
            shade: 0.4,
            title: title,
            content: url,
        });
    }

    function detail(title, url, w, h) {
        if (title == null || title == '') {
            title = false;
        }
        ;
        if (url == null || url == '') {
            url = "error/404";
        }
        ;
        if (w == null || w == '') {
            w = ($(window).width() * 0.9);
        }
        ;
        if (h == null || h == '') {
            h = ($(window).height() - 50);
        }
        ;
        layer.open({
            id: 'user-detail',
            type: 2,
            area: [w + 'px', h + 'px'],
            fix: false,
            maxmin: true,
            shadeClose: true,
            shade: 0.4,
            title: title,
            content: url + '&detail=true',
            // btn:['关闭']
        });
    }

    /**
     * 更新用户
     */
    function update(title, url, w, h) {
        if (title == null || title == '') {
            title = false;
        }
        if (url == null || url == '') {
            url = "404.html";
        }
        if (w == null || w == '') {
            w = ($(window).width() * 0.9);
        }
        if (h == null || h == '') {
            h = ($(window).height() - 50);
        }
        layer.open({
            id: 'user-update',
            type: 2,
            area: [w + 'px', h + 'px'],
            fix: false,
            maxmin: true,
            shadeClose: false,
            shade: 0.4,
            title: title,
            content: url + '&detail=false'
        });
    }

    /*弹出层*/
    /*
     参数解释：
     title   标题
     url     请求的url
     id      需要操作的数据id
     w       弹出层宽度（缺省调默认值）
     h       弹出层高度（缺省调默认值）
     */
    function add(title, url, w, h) {
        if (title == null || title == '') {
            title = false;
        }
        ;
        if (url == null || url == '') {
            url = "404.html";
        }
        ;
        if (w == null || w == '') {
            w = ($(window).width() * 0.9);
        }
        ;
        if (h == null || h == '') {
            h = ($(window).height() - 50);
        }
        ;
        layer.open({
            id: 'user-add',
            type: 2,
            area: [w + 'px', h + 'px'],
            fix: false,
            maxmin: true,
            shadeClose: false,
            shade: 0.4,
            title: title,
            content: url
        });
    }


    /*弹出层*/
    /*
     参数解释：
     title   标题
     url     请求的url
     id      需要操作的数据id
     w       弹出层宽度（缺省调默认值）
     h       弹出层高度（缺省调默认值）
     */
    function tz(title, url, w, h) {
        if (title == null || title == '') {
            title = false;
        }
        ;
        if (url == null || url == '') {
            url = "404.html";
        }
        ;
        if (w == null || w == '') {
            w = ($(window).width() * 0.9);
        }
        ;
        if (h == null || h == '') {
            h = ($(window).height() - 50);
        }
        ;
        alert(1111);
        window.location.href = "/goRegister";
    }

    function reloadTable(){
        var uname = $('#uname').val();
        var email = $('#email').val();
        var realName = $("#realName").val();
        var roleId = $("#roleId").val();
        var auditStatus = $("#auditStatus").val();
        table.reload('userList', {
            where: {
                username: uname,
                email: email,
                realName: realName,
                roleId: roleId,
                auditStatus: auditStatus
            }
        });
    }

    /*导出用户角色*/
    function exportUserRoles(){
        var dep = [];
        // 先去查询部门的数据选择部门
        layerAjaxGet('/search/userRoles/selectDepartment',{},'',function (d) {
            if (d.flag){
                var strContent = "";
                var t = "<div style='width: 80%;height: 30px;margin: 0 auto'><select id='deptCode' style='width: 100%;height: 100%' name='deptName'>";

                var w = "</select></div>";
                for (var i = 0; i < d.data.length;i++){
                    strContent += "<option value="+d.data[i].badtCode+">"+d.data[i].badtName+"</option>";
                }

                layer.open({
                    title: '选择部门',
                    type: 1,
                    shadeClose: false,
                    area: ['300px','200px'],
                    content:t + strContent + w,
                    btn: ['确定','关闭'],
                    yes: function (index){
                        //当点击‘确定’按钮的时候，获取弹出层返回的值
                        // var tempType = $("input[type='radio']:checked").val();
                        var tempType = $("#deptCode option:selected").val();
                        //打印返回的值，看是否有我们想返回的值。
                        console.log(tempType);

                        var url = '/search/userRoles/exportUserRoles?deptName='+tempType+'';
                        location.href = url;
                        layer.closeAll();
                    },
                    btn2: function () {
                        layer.closeAll();
                    }
                })


            }else {
                layer.msg(d.msg, {icon: 2});
                setTimeout(function (){
                    window.location.reload();
                },1000)
            }
        })
    }

</script>
</body>

</html>
